package com.ccl.iot.template;

import java.lang.reflect.Method;
import java.util.ArrayList;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

import com.ccl.iot.publics.Debugger;

public abstract class StandardActivity extends FragmentActivity {
    public static final int KACTIVITY_STATE_INITIALIZED = 0;
    public static final int KACTIVITY_STATE_CREATING = 1;
    public static final int KACTIVITY_STATE_STARTING = 2;
    public static final int KACTIVITY_STATE_RESUMING = 3;
    public static final int KACTIVITY_STATE_SHOWING = 4;
    public static final int KACTIVITY_STATE_PAUSED = 5;
    public static final int KACTIVITY_STATE_STOPPED = 6;
    public static final int KACTIVITY_STATE_DESTORYED = 7;

    /**
     * 状态改变的监听接口
     */
    public interface OnActivityStateChangeListener {
        public void onActivityStateChange(StandardActivity aActivity, int aState);
    }

    /*
     * Static functions
     */
    private static StandardActivity gShowing = null;
    private static ArrayList<Activity> gRunning = null;
    private static OnActivityStateChangeListener gListener = null;

    public static StandardActivity getShowing() {
        return gShowing;
    }

    /**
     * 设置状态改变的监听
     *
     * @param aListener
     */
    public static void SetActivityStateChangeListener(OnActivityStateChangeListener aListener) {
        if (aListener != gListener) {
            gListener = aListener;
        }
    }

    /*
     * 	Non-static functions
     */
    private int iState = KACTIVITY_STATE_INITIALIZED;
    private long iLastStateTime = System.currentTimeMillis();

    private WakeLock iWakeLock = null;
    private PowerManager iPowerManager = null;
    private KeyguardManager iKeyguardManager = null;

    public int getState() {
        return iState;
    }

    public long getLastStateTime() {
        return iLastStateTime;
    }

    private void changeState(int aState) {
        iState = aState;
        iLastStateTime = System.currentTimeMillis();

        if (gListener != null) {
            final OnActivityStateChangeListener tListener = gListener;

            if (tListener != null) {
                tListener.onActivityStateChange(this, aState);
            }
        }
    }

    public boolean isScreenOn() {
        if (iPowerManager == null) {
            iPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        }

        if (iPowerManager != null) {
            return iPowerManager.isScreenOn();
        }

        return false;
    }

    public boolean isKeyguardOn() {
        if (iKeyguardManager == null) {
            iKeyguardManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
        }

        if (iKeyguardManager != null) {
            return iKeyguardManager.inKeyguardRestrictedInputMode();
        }

        return false;
    }

    public Context getContext() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        changeState(KACTIVITY_STATE_CREATING);

        Debugger.Print("onCreate : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");
    }

    @Override
    protected void onStart() {
        super.onStart();

        changeState(KACTIVITY_STATE_STARTING);

        Debugger.Print("onStart : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");

        if (gRunning == null) {
            gRunning = new ArrayList<Activity>();
        }

        if (gRunning.contains(this) == false) {
            gRunning.add(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        changeState(KACTIVITY_STATE_RESUMING);

        Debugger.Print("onResume : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");

        if (iWakeLock != null) {
            if (iWakeLock.isHeld() == false) {
                iWakeLock.acquire();
            }
        }

        changeState(KACTIVITY_STATE_SHOWING);
        gShowing = this;
    }

    @Override
    protected void onPause() {
        super.onPause();
        changeState(KACTIVITY_STATE_PAUSED);

        Debugger.Print("onPause : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");

        if (iWakeLock != null) {
            if (iWakeLock.isHeld()) {
                iWakeLock.release();
            }
        }
        gShowing = null;
    }

    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();

        Debugger.Print("onUserLeaveHint : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");
    }

    @Override
    protected void onStop() {
        super.onStop();
        changeState(KACTIVITY_STATE_STOPPED);
        Debugger.Print("onStop : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        changeState(KACTIVITY_STATE_DESTORYED);
        Debugger.Print("onDestroy : " + this.getClass().getSimpleName() + "(" + this.hashCode() + ")");

        if (gRunning != null) {
            gRunning.remove(this);

            Debugger.Print("Running activity is : " + gRunning.size());
        }
    }

    /**
     * 设置是否保持屏幕被唤醒
     *
     * @param aKeep
     */
    public void setKeepScreenOn(boolean aKeep) {
        if (aKeep) {
            if (iWakeLock == null) {
                PowerManager tPower = (PowerManager) this.getSystemService(Context.POWER_SERVICE);

                iWakeLock = tPower.newWakeLock(PowerManager.FULL_WAKE_LOCK, this.getClass().getSimpleName());

                iWakeLock.acquire();
            } else if (iWakeLock.isHeld() == false) {
                iWakeLock.acquire();
            }
        } else {
            if (iWakeLock != null) {
                if (iWakeLock.isHeld()) {
                    iWakeLock.release();
                }

                iWakeLock = null;
            }
        }
    }

    /**
     * 设置菜单的图标是否可见
     *
     * @param aMenu
     * @param aEnable
     */
    private void setMenuIconEnable(Menu aMenu, boolean aEnable) {
        try {
            Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
            Method tMethod = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
            tMethod.setAccessible(true);

            tMethod.invoke(aMenu, aEnable);
        } catch (Exception e) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setMenuIconEnable(menu, true);

        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 获取存在栈中的activity数量
     *
     * @return
     */
    public static int getRunningCount() {
        if (gRunning != null) {
            return gRunning.size();
        }

        return 0;
    }

}
